local skel = fk.CreateSkill {
  name = "rmt__hujiao",
}

Fk:loadTranslationTable{
  ["rmt__hujiao"] = "胡搅",
  [":rmt__hujiao"] = "出牌阶段，你使用牌时，可令之随机指定目标。结束阶段，你依次视为使用本局你使用过的“胡搅”牌每牌名各一张，且均须触发“胡搅”。",

  ["#rmt__hujiao-invoke"] = "胡搅：是否令%arg随机指定目标？",
  ["@$rmt__hujiao"] = "胡搅",
}

skel:addEffect(fk.PreCardUse, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return (player.phase == Player.Play or table.contains(data.card.skillNames, skel.name)) and data.tos and #data.tos > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    if table.contains(data.card.skillNames, skel.name) then
      return true
    else
      return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#rmt__hujiao-invoke:::"..data.card:toLogString() })
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.phase == Player.Play then
      room:addTableMarkIfNeed(player, "@$rmt__hujiao", data.card.name)
    end
    local orig_tos = table.simpleClone(data.tos)
    -- 先删光原目标再说
    data:removeAllTargets()
    local extra_data = {bypass_distances = true, bypass_times = true}
    local targets = table.filter(room.alive_players, function (to)
      return not player:isProhibited(to, data.card) and
      -- 延时锦囊可以贴自己头上……
      (data.card.sub_type == Card.SubtypeDelayedTrick or data.card.skill:modTargetFilter(data.from, to, {}, data.card, extra_data))
    end)
    -- 用于记录每个目标合法的副目标
    local sub_targets_map = {}
    -- 如果此牌需要副目标，那还得筛选副目标合法性
    if data.card.skill:getMinTargetNum(data.from) > 1 then
      for i = #targets, 1, -1 do
        local to = targets[i]
        local valid_sub_tos = table.filter(room:getOtherPlayers(to, false), function (to2)
          return data.card.skill:targetFilter(data.from, to2, {to}, data.card.subcards, data.card, extra_data)
        end)
        if #valid_sub_tos > 0 then
          sub_targets_map[to] = valid_sub_tos
        else
          table.remove(targets, i)
        end
      end
    end
    -- 没有合法目标则取消使用
    if #targets == 0 then
      return false
    end
    -- 单目标的牌则随机选1个新目标
    if #orig_tos == 1 then
      local random_target = table.random(targets)
      -- 转圈圈动画
      if #targets > 1 then
        for i = 1, 2, 1 do
          for _, p in ipairs(room:getAllPlayers()) do
            room:setEmotion(p, "./image/anim/selectable")
            room:notifyMoveFocus(p, skel.name)
            room:delay(300)
          end
        end
        for _, p in ipairs(room:getAllPlayers()) do
          if table.contains(targets, p) then
            room:setEmotion(p, "./image/anim/selectable")
            room:delay(500)
            if p == random_target then
              room:doIndicate(data.from, {random_target})
              break
            end
          end
        end
      end
      local sub_to = sub_targets_map[random_target] and {table.random(sub_targets_map[random_target])} or nil
      data:addTarget(random_target, sub_to)
    else
      -- 若原目标数为X>1，则随机选择1~X个新目标
      local new_targets = table.random(targets, math.random(1, #orig_tos))
      for _, to in ipairs(new_targets) do
        local sub_to = sub_targets_map[to] and {table.random(sub_targets_map[to])} or nil
        data:addTarget(to, sub_to)
      end
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and player.phase == Player.Finish then
      return table.find(player:getTableMark("@$rmt__hujiao"), function (name)
        local card = Fk:cloneCard(name)
        card.skillName = skel.name
        return card.type ~= Card.TypeEquip and card.sub_type ~= Card.SubtypeDelayedTrick and
          player:canUse(card, {bypass_distances = true, bypass_times = true})
      end)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local names = table.simpleClone(player:getTableMark("@$rmt__hujiao"))
    for _, name in ipairs(names) do
      if player.dead then return end
      local card = Fk:cloneCard(name)
      card.skillName = skel.name
      if card.type ~= Card.TypeEquip and card.sub_type ~= Card.SubtypeDelayedTrick then
        local tos = card:getDefaultTarget(player, {bypass_distances = true, bypass_times = true})
        if #tos > 0 then
          room:useCard{
            from = player,
            tos = tos,
            card = card,
            extraUse = true,
          }
        end
      end
    end
  end,
})

return skel
